<script lang="ts" setup>
import type { StateType } from './types';

import { computed } from 'vue';
import { useRoute } from 'vue-router';

import { Page } from '@vben/common-ui';
import { useTabs } from '@vben/hooks';

import { useTitle } from '@vueuse/core';
import {
  Card,
  message,
  Select,
  SelectOption,
  TabPane,
  Tabs,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import { pinyin } from 'pinyin-pro';

import { useVbenForm } from '#/adapter/form';
import { getPageInfo, postPage, putPage } from '#/api/admin/translate';
import { usePreferencesStore } from '#/store';

const route = useRoute();

const { setTabTitle, closeCurrentTab } = useTabs();

const index: any = computed(() => {
  return route.params?.id ?? -1;
});
const router = useRouter();

const routeMeta = router.currentRoute.value.meta;
const routeTitle = routeMeta.title.replace('管理', '');
const preferencesStore = usePreferencesStore();
const { title } = storeToRefs(preferencesStore);
const pageTitle =
  Number.parseInt(index.value, 10) === 0
    ? `新增${routeTitle}`
    : `No.${index.value} - 编辑${routeTitle}`;
setTabTitle(pageTitle);
useTitle(`${pageTitle} - ${title.value}`);

const disabled = ref(true);
const activeKey = ref('1');
const state: StateType = reactive({
  form: {
    post_title: '',
    post_alias: '',
    more: {
      thumbnail: '',
    },
  },
  pageThemeFiles: [],
});

const [BasicForm, BasicFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      defaultValue: '',
      dependencies: {
        trigger: (values) => {
          disabled.value = !values.table_name;
          state.form.table_name = values.table_name;
          state.form.record_id = values.record_id;
          state.form.field_name = values.field_name;
          state.form.lang_code = values.lang_code;
          state.form.field_value = values.field_value;
          state.form.old_value = values.old_value;
        },
        // 随意一个字段改变时，都会触发
        triggerFields: [
          'id',
          'table_name',
          'record_id',
          'field_name',
          'lang_code',
          'field_value',
          'old_value',
        ],
      },
      componentProps: {
        placeholder: '请输入',
        disabled: true, // 禁止输入
      },
      fieldName: 'table_name',
      label: `${routeTitle}标题`,
      rules: 'required',
    },

    {
      component: 'Input',
      defaultValue: '',
      componentProps: {
        placeholder: '请输入',
        disabled: true, // 禁止输入
      },
      fieldName: 'record_id',
      label: `record_id`,
    },

    {
      component: 'Input',
      defaultValue: '',
      componentProps: {
        placeholder: '请输入',
        disabled: true, // 禁止输入
      },
      fieldName: 'field_name',
      label: 'field_name',
    },
        {
      component: 'Input',
      defaultValue: '',
      componentProps: {
        placeholder: '请输入',
        disabled: true, // 禁止输入
      },
      fieldName: 'lang_code',
      label: 'lang_code',
    },
        {
      component: 'Textarea',
      defaultValue: '',
      componentProps: {
        placeholder: '请输入',
        disabled: true, // 禁止输入
        style: {
            height: '300px'
        }
      },
     
      fieldName: 'old_value',
      label: '原始数据',
    }, 
    {
      component: 'Textarea',
      defaultValue: '',
      componentProps: {
        placeholder: '请输入',
        style: {
        height: '300px'
    }
      },
      fieldName: 'field_value',
      label: '翻译数据',
    },
  ],
});



async function onSubmit() {

  function showMsg(msg: string) {
    message.success(msg);
    closeCurrentTab();
      router.push({
          name: `Admintranslate`,
     });
  }
  const values: any = state.form;

  if (values.id) {
    const { msg } = await putPage(values);
    showMsg(msg);
  } else {
    delete values.id;
    const { msg } = await postPage(values);
    showMsg(msg);
  }
}
async function onGetPage(id: number) {
  const { data } = await getPageInfo({ id });


  data.post = {
    id: data?.id ?? undefined,
    table_name: data?.table_name ?? '',
    record_id: data?.record_id ?? '',
    field_name: data?.field_name ?? '',
    lang_code: data?.lang_code ?? '',
    field_value: data?.field_value ?? '',
     old_value: data?.old_value ?? '',
  };
  state.form = data.post;
  BasicFormApi.setValues({ ...state.form});
  state.pageThemeFiles = data.pageThemeFiles;
}

onMounted(async () => {
  if(index.value>0){
          await onGetPage(index.value);
  }
  
});
</script>

<template>
  <Page>
    <Card :title="`${routeTitle}详情`">
      <BasicForm />
    </Card>
  </Page>
</template>
